// ActionScript file
import common.events.GenericEvent;
import common.utils.StringManager;
import common.utils.Constants;

//import flash.utils.Dictionary;

import login.components.Application;
import login.dataObjects.SessionDO;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.managers.HistoryManager;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;


private var hist:HistoryManager;
private var _sessID:String;
private var _userID:String;
private var _webConfig:XML;
private var _defaultWhs:String;
private var _appServer:String;// = "http://116.227.194.190/SCV_APP/";
private var _rptServer:String;// = "http://116.227.194.190/SCV_RPT/";
private var _webServer:String;// = "http://116.227.194.190/SCV_RPT/";
private var _acCompany:ArrayCollection = new ArrayCollection();
private var _acInboundSts:ArrayCollection = new ArrayCollection();
private var _acOutboundSts:ArrayCollection = new ArrayCollection();
private var _acLocatingRule:ArrayCollection = new ArrayCollection();
private var _acAllocationRule:ArrayCollection = new ArrayCollection();
private var _acInventorySts:ArrayCollection = new ArrayCollection();
private var _acGlobalCache:ArrayCollection = new ArrayCollection();

private function initData():void{
	configFile.url = "login/config/Config.xml";
	configFile.send();
}

private function onGetConfigResult(event:ResultEvent):void{
	this._webConfig = new XML(event.result.toString());
	
	this._appServer = _webConfig.AppServer;
	this._rptServer = _webConfig.RptServer;
	this._webServer = _webConfig.WebServer;
	
	var wsdl:String =  _appServer + "GeneralInforService.asmx?wsdl";
	GeneralInfoSvs.loadWSDL(wsdl);
	
	GeneralInfoSvs.ValidateLicense();
}

private function onValidateResult(event:ResultEvent):void{
	if (event.result != null){
		var errMsg:String = event.result.toString();
		if (StringManager.IsErrorMsg(errMsg)){
			Alert.show(errMsg, "错误");
			this.removeChild(lgn);
		}
		else{
			if (errMsg != null && errMsg.length > 0)
				Alert.show(errMsg, "信息");
		}
	}
}

private function logSucceed(event:GenericEvent):void{
	var xmlObj:XML = event.obj as XML;
	_sessID = xmlObj.SESSION_ID.text();
	_userID = xmlObj.USER_ID.text();
	
	//以下获取全局缓存数据
	GeneralInfoSvs.GetDefaultWarehouse(_sessID, _userID);
	GeneralInfoSvs.GetCompanies(_sessID, "");
	if (this._acInboundSts.length == 0)
		GeneralInfoSvs.GetInboundSts(_sessID);
	if (this._acOutboundSts.length == 0)
		GeneralInfoSvs.GetOutboundSts(_sessID);
	if (this._acLocatingRule.length == 0)
		GeneralInfoSvs.GetLocatingRules(_sessID);
	if (this._acAllocationRule.length == 0)
		GeneralInfoSvs.GetAllocationRules(_sessID);
	if (this._acAllocationRule.length == 0)
		GeneralInfoSvs.GetInventorySts(_sessID);	
	
}

private function setDefaultWhs(event:ResultEvent):void{
	var errMsg:String;
	
	if (event.result == null){	
		Alert.show("错误：网络服务未返回结果");
		return;
	}
	//
	errMsg = event.result as String;
	if (StringManager.IsErrorMsg(errMsg)){
		Alert.show(errMsg);
	}
	else{
		_defaultWhs = errMsg;
		currentState = 'mainApp';
	}
}			

private function setCompany(event:ResultEvent):void{
	var errMsg:String;
	
	if (event.result == null){	
		Alert.show("错误：网络服务未返回结果");
		return;
	}
	//
	errMsg = event.result as String;
	if (StringManager.IsErrorMsg(errMsg)){
		Alert.show(errMsg);
	}
	else{
		var xmlObj:XML = new XML(errMsg);
		_acCompany.removeAll();
		_acCompany.addItem({label:"全部", data:""});
		for each(var xmlNode:XML in xmlObj.children()){
			_acCompany.addItem({label:xmlNode.NAME.toString(), data:xmlNode.COMPANY.toString()});
		}
	}
}

private function onFault(event:FaultEvent):void{
	Alert.show("网络服务失败：" + event.message.toString());
}

private function bindInboundSts(event:ResultEvent):void{
	var errMsg:String;				
	if (event.result != null){
		errMsg = event.result as String;
		if (StringManager.IsErrorMsg(errMsg)){
			Alert.show(errMsg);
		}
		else{
			var xmlObj:XML = new XML(errMsg);
			this._acInboundSts.removeAll();
			//_acInboundSts.addItem({label:"所有状态", data:""});
			for each(var xmlNode:XML in xmlObj.children()){
				this._acInboundSts.addItem({label:xmlNode.DESCRIPTION.toString(), data:xmlNode.IDENTIFIER.toString()});
			}
		}
	}
}

private function bindOutboundSts(event:ResultEvent):void{
	var errMsg:String;				
	if (event.result != null){
		errMsg = event.result as String;
		if (StringManager.IsErrorMsg(errMsg)){
			Alert.show(errMsg);
		}
		else{
			var xmlObj:XML = new XML(errMsg);
			this._acOutboundSts.removeAll();
			_acOutboundSts.addItem({label:"所有状态", data:""});
			for each(var xmlNode:XML in xmlObj.children()){
				this._acOutboundSts.addItem({label:xmlNode.DESCRIPTION.toString(), data:xmlNode.IDENTIFIER.toString()});
			}
		}
	}
}

private function bindLocatingRule(event:ResultEvent):void{
	var errMsg:String;				
	if (event.result != null){
		errMsg = event.result as String;
		if (StringManager.IsErrorMsg(errMsg)){
			Alert.show(errMsg);
		}
		else{
			var xmlObj:XML = new XML(errMsg);
			this._acLocatingRule.removeAll();
			//_acInboundSts.addItem({label:"所有状态", data:""});
			for each(var xmlNode:XML in xmlObj.children()){
				this._acLocatingRule.addItem({label:xmlNode.LOCATING_RULE.toString(), data:xmlNode.LOCATING_RULE.toString()});
			}
		}
	}
}

private function bindAllocationRule(event:ResultEvent):void{
	var errMsg:String;				
	if (event.result != null){
		errMsg = event.result as String;
		if (StringManager.IsErrorMsg(errMsg)){
			Alert.show(errMsg);
		}
		else{
			var xmlObj:XML = new XML(errMsg);
			this._acAllocationRule.removeAll();
			//_acInboundSts.addItem({label:"所有状态", data:""});
			for each(var xmlNode:XML in xmlObj.children()){
				this._acAllocationRule.addItem({label:xmlNode.ALLOCATION_RULE.toString(), data:xmlNode.ALLOCATION_RULE.toString()});
			}
		}
	}
}

private function bindInventorySts(event:ResultEvent):void{
	var errMsg:String;				
	if (event.result != null){
		errMsg = event.result as String;
		if (StringManager.IsErrorMsg(errMsg)){
			Alert.show(errMsg);
		}
		else{
			var xmlObj:XML = new XML(errMsg);
			this._acInventorySts.removeAll();
			//_acInboundSts.addItem({label:"所有状态", data:""});
			for each(var xmlNode:XML in xmlObj.children()){
				this._acInventorySts.addItem({label:xmlNode.DESCRIPTION.toString(), data:xmlNode.IDENTIFIER.toString()});
			}
		}
	}
}

private function showLogin():void{
	this._sessID = null;
	this.currentState = '';
	this.lgn.btnLogin.enabled = true;
	this.lgn.btnOK.enabled = true;
}

public function get SessionID():String{
	return this._sessID;
}

public function get UserID():String{
	return this._userID;
}

public function get WebServer():String{
	return this._webServer;
}

public function get AppServer():String{
	return this._appServer;
}

public function get ReportServer():String{
	return this._rptServer;
}

public function get CurrentWarehouse():String{
	return this._defaultWhs;
}

public function get CompanyList():ArrayCollection{
	return this._acCompany;
}

public function get InboundStsList():ArrayCollection{
	return this._acInboundSts;
}

public function get InventoryStsList():ArrayCollection{
	return this._acInventorySts;
}

public function get OutboundStsList():ArrayCollection{
	return this._acOutboundSts;
}

public function get LocatingRuleList():ArrayCollection{
	return this._acLocatingRule;
}

public function get AllocationRuleList():ArrayCollection{
	return this._acAllocationRule;
}
